package org.taishan.electronshop.dao;

import org.taishan.electronshop.domain.Goods;
import org.taishan.electronshop.domain.Order;
import org.taishan.electronshop.domain.OrderItem;
import org.taishan.electronshop.domain.TrolleyItem;
import org.taishan.electronshop.util.JdbcHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class OrderItemDao implements Dao<OrderItem,Integer>{
    private OrderItemDao oid;
    private GoodsDao gd=new GoodsDao();
    private OrderDao od = new OrderDao();
    private static final String table = " t_order_items " ;
    @Override
    public OrderItem load(Integer oiid) {
        return null;
    }

    public OrderItem load(String oid) {
        String SQL = "SELECT   id,goods_id,price, goods_number,order_id FROM" + table  + "WHERE order_id = ? " ;
        JdbcHelper helper = new JdbcHelper();
        List<Map<String,Object>> list = helper.query( SQL , oid ) ;
        helper.destory();
        if( list != null && list.size() == 1 ) {
            Map<String,Object> map = list.get( 0 );
            OrderItem oit =   this.cast( map );
            return oit ;
        }
        return null;
    }


    @Override
    public List<OrderItem> list() {
        String SQL = "SELECT   id,goods_id,price, goods_number,order_id FROM" + table   ;
        JdbcHelper helper = new JdbcHelper();
        List<Map<String,Object>> list = helper.query( SQL  ) ;
        helper.destory();
        if( list != null && list.size() >0) {
            List<OrderItem> orderItems= new ArrayList<>();
            for( int i = 0 ; i < list.size() ; i++ ) {
                Map<String, Object> map = list.get(i);
                OrderItem oi =   this.cast( map );
                orderItems.add(oi);
            }
            return orderItems;
        }
        return null;
    }

    @Override
    public boolean save(OrderItem oit) {
        String SQL = "INSERT INTO " + table +
                " ( goods_name, goods_id , price , goods_number,order_id   )  " +
                " VALUES (  ? , ? , ? , ? ,? )" ;
        Goods g = oit.getGoods();

        String goodsname = oit.getName();
        Integer goodsId = g.getId();
        System.out.println(goodsId);
        double price = g.getPrice();
        int goodsNumber = oit.getNumber();
        Order o = oit.getOrder();
        String orderId = o.getId();
        System.out.println(orderId);
        Object[] array = {goodsname,goodsId,price,goodsNumber,orderId};
        JdbcHelper helper = new JdbcHelper();
        // 执行插入语句，并获得由数据库产生的主键值
        int id = helper.insert( SQL , array );

        oit.setId( id );
        helper.destory();
        // 如果 id 不等于 -1  则视为保存成功( 在 JdbcHelper 的 insert 方法中有约定 )
        return id != -1 ;
    }

    @Override
    public boolean update(OrderItem o) {
        return false;
    }

    @Override
    public boolean delete(Integer id) {
        return false;
    }

    /**
     * 将从数据库中获取的数据存入到Order对象中
     */
    @Override
    public OrderItem cast(Map map) {
        OrderItem oi = new OrderItem();
        oi.setId((Integer)map.get("id"));
        Goods g =gd.load((Integer) map.get("goods_id"));
        oi.setName(g.getName());
        oi.setPrice((double) map.get("price"));
        oi.setGoods(g);
        Order o = od.load((String) map.get( "order_id" ));
        oi.setOrder(o);
        oi.setNumber((Integer) map.get("goods_number"));
        return oi;
    }
}
